/*
 Exercise 8.3
 Create an analogue display clock
 http://tronixstuff.com/tutorials > Chapter Eight
 
 based on code by Maurice Ribble
 17-4-2008 - http://www.glacialwanderer.com/hobbyrobotics

 Set time with 24 hour time only. 

 
 */

#include "Wire.h"
#include "Servo.h"

Servo hourservo;
Servo minuteservo;

#define DS1307_I2C_ADDRESS 0x68

int realhour = 0;
int b = 0;
int c= 0;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void setDateDs1307(byte second,byte minute,byte hour,byte dayOfWeek,byte dayOfMonth,byte month,byte year)
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));     
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.send(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
  Wire.endTransmission();
}

void getDateDs1307(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month,byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}

byte getserialnumber()
  // function to get an integer from the serial monitor box, i.e. user input
  {
    byte result=0;
    byte q = 0;
    while (Serial.available()==0)
    {
      // do nothing until something comes into the serial buffer
    } 
    while (Serial.available()>0)
    {
      while (Serial.available()>0)
      {
        result=result*10; // move the previous digit to the next column on the left, e.g. 1 becomes 10
        // while there is data in the buffer
        q = Serial.read()-48; // read the next number in the buffer, subtract 48 to convert to the actual number
        result=result+q;
      }
      delay(5); // the processor needs a moment to process
    }
    return result;
  }


void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  Serial.begin(9600);
  
  hourservo.attach(9);
  minuteservo.attach(6);

  second = 0;
  dayOfWeek = 1;
  dayOfMonth = 24;
  month = 5;
  year = 10;
  
  Serial.println("Clock setting. Please enter hours (1~12):");
  hour=getserialnumber();
  Serial.println(" and minutes: (0~59):");
  minute=getserialnumber();
  Serial.println();
  Serial.print("Setting clock to ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.println(minute, DEC);
  

  
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  realhour = int(hour);

  if (realhour==0) // if it is 0000h, that should be 1200h in 12 hour time
  {
    realhour=12;
  }
  if (hour>=13)
  {
      realhour=realhour-12; // convert 24h hours to 12h hours
  }
  b=realhour*14;
  c=minute*3;
  hourservo.write(b);
  delay(100);     // servos need time to move
  minuteservo.write(c);
  delay(100);
  
}





